function formatTime(date,format) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
  
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
  
  
   // return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
    if(format == 'yyyy-MM-dd'||format == ''){
        return [year, month, day].map(formatNumber).join('-') 
    }
    if(format == 'yyyy-MM'){
        return [year, month].map(formatNumber).join('-') 
    }
    if(format == 'yyyy-MM-dd HH:mm:ss'){
      return ([year, month, day].map(formatNumber).join('-'))+' '+ ([hour, minute, second].map(formatNumber).join(':'))
    }
  }
  
function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }

function lastMonth(){
  let _time = new Date(formatTime(new Date(),'yyyy-MM')).getTime() - 86400000
  return formatTime(new Date(_time),'yyyy-MM') 
}
  
module.exports = {
  formatTime: formatTime,
  lastMonth: lastMonth
}